package com.wlc.thread.demo05;

/**
 * @author 王立朝
 * @description 测试线程
 * @date 2021-11-10 23:56:13
 */
public class TestThread {

    public void method01() throws InterruptedException {
        Thread t1 = new Thread(() -> System.out.println("开启了一个线程1"));
        t1.start();
        t1.setPriority(1);

        Thread t2 = new Thread(() -> System.out.println("开启了一个线程2"));
        t2.start();
        t2.setPriority(3);

        t2.join();
    }


    public void sleepDemo() {
        int minute = 0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                int minute = 0;
                while (true) {
                    try {
                        Thread.sleep(1000);
                        System.out.println("运行了。。。" + minute++);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.out.println("发生了异常");
                    }

                }
            }
        }).start();
    }

    public void joinDemo() {
        System.out.println("在主线程中运行");
        Thread t1 = new Thread(() -> {
            int num2 = 0;
            while (true) {
                //try {
                //    Thread.sleep(1000);
                //} catch (InterruptedException e) {
                //    e.printStackTrace();
                //}
                System.out.println("这是 t1 " + num2++);
            }

        });

        t1.start();
        t1.setPriority(Thread.MIN_PRIORITY);
        System.out.println("继续执行1");

        Thread t2 = new Thread(() -> {
            int num = 0;
            while (true) {
                //try {
                //    Thread.sleep(1000);
                //} catch (InterruptedException e) {
                //    e.printStackTrace();
                //}
                System.out.println("这是  t2  n" + num++);

                // yield 可以让线程让出暂时让出，有可运行状态转换为就绪状态
                Thread.yield();
            }
        });
        t2.start();
        t2.setPriority(Thread.MAX_PRIORITY);

        //try {
        //    t1.join();
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}

        System.out.println("继续执行主线程4");

    }

    public static void main(String[] args) throws InterruptedException {
        TestThread t = new TestThread();
        t.joinDemo();
    }
}
